//
//  UpdownloadManage.m
//  BaseProject
//
//  Created by Macx on 2018/3/29.
//  Copyright © 2018年 Gavin. All rights reserved.
//

#import "UpdownloadManage.h"
#import "ProjectAPI.h"
#import <pthread/pthread.h>
#import "NetworkConfig.h"

#if __has_include(<AFNetworking/AFNetworking.h>)
#import <AFNetworking/AFNetworking.h>
#else
//#import "AFNetworking.h"
#import <AFNetworking/AFHTTPSessionManager.h>
#endif

#define Lock() pthread_mutex_lock(&_lock)
#define Unlock() pthread_mutex_unlock(&_lock)


@interface UpdownloadManage(){
    
    AFHTTPSessionManager *_manager;
    dispatch_queue_t queue;
}

@property (nonatomic ,strong) NSMutableArray  *fileDataModelArray;

@end

@implementation UpdownloadManage


+ (UpdownloadManage *)shareUpdownloadManage{
    
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];

    });
    
    return sharedInstance;
}

- (instancetype)init{
    
    self = [super init];
    
    if (self) {
        queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:[NetworkConfig sharedConfig].baseUrl] sessionConfiguration:configuration];
        _fileDataModelArray = [NSMutableArray array];
    }
    
    return self;
}

- (void)updownload:(UIImage *)image
          argument:(NSDictionary *)arguments
           Success:(nullable RequestCompletionBlock)success
                  failure:(nullable RequestCompletionBlock)failure{
//    dispatch_group_t group =  dispatch_group_create();
//    dispatch_group_enter(group);
    [_manager POST:@"" parameters:arguments constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        [formData appendPartWithFileData:nil name:@"" fileName:@"" mimeType:@""];
//        [formData appendPartWithFormData:nil name:@""];
        // 图片经过等比压缩后得到的二进制文件
        NSData *imageData = UIImageJPEGRepresentation(image,1.f);
        NSString *imageFileName = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeIntervalSinceNow:0]];
        [formData appendPartWithFileData:imageData
                                    name:@"file"
                                fileName:@"image.jpg"
                                mimeType:@""];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 获取主队列
//        dispatch_queue_t mainQueue = dispatch_get_main_queue();
//        // 回到主线程
//        dispatch_async(mainQueue, ^{
//
//        });
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            dispatch_group_leave(group);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            dispatch_group_leave(group);
    }];
    
//    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//        NSLog(@"group---end");
//    });
}

@end
